(() => {
    /** 点击后等待时间  */
    const timeForClick = 1000;
    /** 定时捡饲料 */
    let pickFoodTask = null;
    /** 根据class名称，点击对应节点 */
    function clickByClassName (className, index) {
        let btn = document.getElementsByClassName(className);
        index = index || 0;
        if (btn && btn[index]) {
            btn[index].click();
            return true;
        }
        return false;
    }

    function sleep(timeout) {
        return new Promise(resolve => {
            setTimeout(resolve, timeout);
        });
    }

    console.log('今天你🐖了没0');
    /** 签到 */
    async function signIn () {
        console.log('准备签到');        /** 打开签到 */
        clickByClassName('main-icon main-icon-sign');
        await sleep(timeForClick);
        /** 签到 */
        clickByClassName('sign-btn');
        await sleep(timeForClick);
        /** 关闭签到 */
        clickByClassName('sign-close');
        /** 关闭奖励 */
        clickByClassName('precious-btn');
        console.log('签到完成');
    }
    function pickFood () {
        console.log('开始捡猪饲料');
        let disableFood = document.getElementsByClassName('main-precious-disable')[0];
        if (disableFood) {
            console.log('待补给饲料，停止捡饲料');
            stopPickFood();
        }
        if (pickFoodTask) {
            return;
        }
        pickFoodTask = setInterval(async () => {
            console.log("检查");
            /** 点击左边礼物盒 */
            let res = clickByClassName('precious precious-one precious-pos-1 springback');
            if (!res) {
                /** 点击右边礼物盒 */
                clickByClassName('precious precious-one precious-pos-2 springback');
            }
            console.log("点击礼物盒");
            sleep(timeForClick);
            clickByClassName('precious-btn');
        }, 3000);
    }
    /** 停止捡饲料 */
    function stopPickFood () {
        console.log('停止捡饲料');        if (pickFoodTask) {
            clearInterval(pickFoodTask);
        }
    }

    /** 添加食物 */
    async function addFood () {
        console.log('开始喂食');        /** 打开饲料仓库 */
        clickByClassName('draw-trough-img');
        await sleep(timeForClick);

        /** 如果是没有饲料，直接关闭 */
        let empytBtn = document.querySelector('.empty-btn');
        if (empytBtn) {
            empytBtn.click();
            console.log('没有饲料，退出喂食');
            await sleep(timeForClick);
            return false;
        }
        /** 饲料详情 */
        let foodItems = document.getElementsByClassName('feed-item-desc');
        let foodCounts = new Array();
        for (let i=0; i<foodItems.length; i++) {
            let count = foodItems[i].childNodes[1].innerText;
            foodCounts.push({'index': i, 'count': count});
        }
        /** 饲料按余量降序 */
        foodCounts.sort((a, b) => {
            return b.count - a.count;
        });
        /** 取最大余量喂食 */
        let index = foodCounts[0].index;
        clickByClassName('feed-item-btn', index);
        /** 关闭饲料仓库 */
        clickByClassName('feed-close');
        await sleep(timeForClick);
    }
    /** 是否需要喂食 */
    function isNeedFood () {
        let needBtn = document.getElementsByClassName('draw-feed-btn');
            if (needBtn && needBtn[0]) {
            return true;
        }
        /** 剩余时间 mm:hh:ss 格式 */
        let residue = document.getElementsByClassName('draw-feed-time');
        if (residue && residue[0]) {
            return !/\d{2}:\d{2}:\d{2}/.test(residue[0].innerText);
        }
        return false;
    }    
    /** 抽奖 */
    async function getLucky() {
        let btn = document.querySelector('.foot-icon');
        btn.click();
        console.log('打开抽奖');
        await sleep(2000);
        console.log('已打开抽奖');
        btn = document.querySelector("a[class='luck-btn KinerLotteryBtn start']");
        if (btn) {
            let txt = btn.innerText;
            if (txt.indexOf('免费') == 0) {
                console.log('免费抽奖');
                btn.click();
                await sleep(8000);
                btn = document.querySelectorAll('.precious-btn');
                if (btn && btn[0] && btn[0].innerText == '好的') {
                    btn[0].click();
                    await sleep(timeForClick);
                    getLucky();
                } else {
                    console.log('没有中奖');
                    btn = document.querySelector('.luck-close');
                    btn.click();
                    await sleep(timeForClick);
                }
            } else {
                console.log('免费次数用完，退出抽奖');
                btn = document.querySelector('.luck-close');
                btn.click();
                await sleep(timeForClick);
            }
        } else {
            console.log('没有签到抽奖入口');
            btn = document.querySelector('.luck-close');
            btn.click();
            await sleep(timeForClick);
        }
    }
    /** 协调喂食和捡饲料 */
    function checkAndAddFood () {
        setInterval(() => {
            if (isNeedFood()) {
                stopPickFood();
                addFood();
                pickFood();
            } else {
                console.log('不需要喂食');            }
        }, 3000);
    }

    /** 检查是否可以执行 */
    function checkCanExecute() {
        let war = document.querySelector('.modal-btn.confirm');
        if (war && war.innerText == '在京东金融App领福利') {
            console.log('被限制只能在京东金融App领福利');
            /** alert('无法自动养猪，请设置浏览器代理为手机模式、清除浏览器缓存后，刷新页面重试'); */
            return false;
        }
        return true;
    }
    /** window.addEventListener('load', async () => { */
    async function main() {
        console.log('🐖🐖🐖🐖🐖🐖');
        await sleep(3000);
        let date = new Date();
        if (!checkCanExecute()) {
            console.log(`检测到当前浏览器被限制养猪，15分钟后自动刷新页面重试，当前时间： ${date.getDate()} 日 ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`);
            let tip = document.createElement('span');
            tip.innerText = '自动养猪：检测到当前浏览器被限制，15分钟后自动刷新页面重试';
            tip.style = 'font-size: 20pt;';
            document.body.insertBefore(tip, document.body.children[0]);
            setTimeout(() => {window.location.reload()}, 1000*60*15);
            return false;
        }
        console.log(`开始搞事情 ${date.getDate()} 日 ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`);
        signIn();
        await sleep(5000);
        getLucky();
        pickFood();
        checkAndAddFood();
        /** 下一天凌晨刷新页面，进行新一轮任务 */
        date = new Date();
        date.setDate(date.getDate()+1);
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(1);
        setTimeout(() => {
            window.location.reload();
        }, (date.getTime() - new Date().getTime()));
        console.log(`计划【${date}】 刷新页面`);
    }
    main();
})();